package com.aaa.sbm.controller;

import com.aaa.sbm.entity.Product;
import com.aaa.sbm.service.ProductService;
import com.aaa.sbm.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("product")
public class ProductController {
    
    @Resource
    private ProductService productService;
    
    @GetMapping("")
    public String index() {
        return "html/product";
    }
    
    @PostMapping("add")
    public Result add(@RequestBody Product product) {
        log.info("添加商品，参数：{}", product);
        return productService.addProduct(product);
    }
    
    @PostMapping("update")
    public Result update(@RequestBody Product product) {
        log.info("更新商品，参数：{}", product);
        return productService.updateProduct(product);
    }
    
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除商品，ID：{}", id);
        return productService.deleteProduct(id);
    }
    
    @GetMapping("get/{id}")
    public Result get(@PathVariable Integer id) {
        return productService.getProduct(id);
    }
    
    @PostMapping("list")
    public Result list(@RequestBody(required = false) Product product,
                      @RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询商品列表，参数：product={}, pageNum={}, pageSize={}", product, pageNum, pageSize);
        return productService.getProductList(product, pageNum, pageSize);
    }
    
    @GetMapping("lowStock")
    public Result lowStock() {
        return productService.getLowStockProducts();
    }
} 